{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ccdd4142",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openstudio\n",
    "\n",
    "# pip install fuzzywuzzy[speedup]\n",
    "from fuzzywuzzy import process"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d804b2de",
   "metadata": {},
   "source": [
    "I'm comparing the IDD references for objects referencing curves between E+ and OS IDDs.\n",
    "The fuzzywuzzy python module is used to match the OS field name to the EP IDD one (because we may be outdated or deviated on purpose, like removing numbers when making extensible or removing 'Name' at end)\n",
    "\n",
    "See https://github.com/NREL/OpenStudio/issues/4289"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ea14caed",
   "metadata": {},
   "outputs": [],
   "source": [
    "ep_idd_path = \"../resources/energyplus/ProposedEnergy+.idd\"\n",
    "ep_idd = openstudio.IddFile.load(openstudio.toPath(ep_idd_path)).get()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5f9dd5c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "os_idd_path = \"../resources/model/OpenStudio.idd\"\n",
    "os_idd = openstudio.IddFile.load(openstudio.toPath(os_idd_path)).get()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fc3c0c9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'AllCurves',\n",
       " 'BivariateFunctions',\n",
       " 'MultivariateFunctions',\n",
       " 'QuadvariateFunctions',\n",
       " 'QuintvariateFunctions',\n",
       " 'TrivariateFunctions',\n",
       " 'UnivariateFunctions'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "curve_objects = [x for x in os_idd.objects() if (\"Curve:\" in x.name()) or (\"Table:\") in x.name()]\n",
    "set([x for curve in curve_objects for x in curve.references() if not \"UniqueNames\" in x])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5797dcbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "CURVE_REFS = {\n",
    "    \"AllCurves\",\n",
    "    \"BivariateFunctions\",\n",
    "    \"MultivariateFunctions\",\n",
    "    \"QuadvariateFunctions\",\n",
    "    \"QuintvariateFunctions\",\n",
    "    \"TrivariateFunctions\",\n",
    "    \"UnivariateFunctions\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3cf63f5e",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "failed to locate in EP IDD for OS:ElectricEquipment:ITE:AirCooled:Definition\n",
      "failed to locate in EP IDD for OS:Coil:Cooling:FourPipeBeam\n",
      "failed to locate in EP IDD for OS:Coil:Heating:FourPipeBeam\n",
      "failed to locate in EP IDD for OS:Coil:Cooling:DX:MultiSpeed:StageData\n",
      "failed to locate in EP IDD for OS:Coil:Heating:Gas\n",
      "failed to locate in EP IDD for OS:Coil:Heating:DX:MultiSpeed:StageData\n",
      "failed to locate in EP IDD for OS:Coil:Heating:DX:VariableSpeed:SpeedData\n",
      "failed to locate in EP IDD for OS:Coil:Cooling:DX:VariableSpeed:SpeedData\n",
      "failed to locate in EP IDD for OS:Coil:Heating:WaterToAirHeatPump:VariableSpeedEquationFit:SpeedData\n",
      "failed to locate in EP IDD for OS:Coil:Cooling:WaterToAirHeatPump:VariableSpeedEquationFit:SpeedData\n",
      "failed to locate in EP IDD for OS:Coil:WaterHeating:AirToWaterHeatPump\n",
      "failed to locate in EP IDD for OS:Coil:WaterHeating:AirToWaterHeatPump:VariableSpeed:SpeedData\n",
      "failed to locate in EP IDD for OS:Generator:MicroTurbine:HeatRecovery\n",
      "failed to locate in EP IDD for OS:AirflowNetworkExternalNode\n",
      "failed to locate in EP IDD for OS:AirflowNetworkOccupantVentilationControl\n"
     ]
    }
   ],
   "source": [
    "for obj in os_idd.objects():\n",
    "    obj_name = obj.name()\n",
    "    if any(ref in CURVE_REFS for ref in obj.objectLists()):\n",
    "        # print(obj_name)\n",
    "\n",
    "        ep_obj_name = obj_name.replace(\"OS:\", \"\")\n",
    "        o_ep_obj = ep_idd.getObject(ep_obj_name)\n",
    "        if not o_ep_obj.is_initialized():\n",
    "            print(f\"failed to locate in EP IDD for {obj_name}\")\n",
    "            continue\n",
    "\n",
    "        ep_obj = o_ep_obj.get()\n",
    "        ep_obj_field_names = [ep_obj.getField(i).get().name() for i in range(ep_obj.numFields())]\n",
    "\n",
    "        for idx in obj.objectListFields():\n",
    "            field = obj.getField(idx).get()\n",
    "            field_name = props = field.name()\n",
    "\n",
    "            props = field.properties()\n",
    "            refs = set([x for x in props.objectLists if not \"AllCurve\" in x])\n",
    "            if not any(ref in CURVE_REFS for ref in refs):\n",
    "                continue\n",
    "\n",
    "            # print(f\" * {field_name}: {refs}\")\n",
    "\n",
    "            ep_field_name, score = process.extractOne(field_name, ep_obj_field_names)\n",
    "            if score < 80:\n",
    "                print(f\"Bad score for {obj_name} - {ep_field_name}: {score}\")\n",
    "\n",
    "            else:\n",
    "                # print(f\"{score} - {field_name} - {ep_field_name}\")\n",
    "                pass\n",
    "            ep_field_idx = ep_obj_field_names.index(ep_field_name)\n",
    "            ep_field = ep_obj.getField(ep_field_idx).get()\n",
    "            ep_refs = set([x for x in ep_field.properties().objectLists])\n",
    "            extra_os = refs - ep_refs\n",
    "\n",
    "            if extra_os:\n",
    "                print(f\"WRONG extra OS {obj_name} - {field_name}: {refs=}, {ep_refs=}, {ep_field_name=}\")\n",
    "\n",
    "            extra_ep = ep_refs - refs\n",
    "            if extra_ep:\n",
    "                print(f\"WRONG extra EP {obj_name} - {field_name}: {refs=}, {ep_refs=}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b83e3512",
   "metadata": {},
   "source": [
    "These above I manually checked"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
